﻿Imports System.Data.SqlClient

Public Class FrmCapNhatLuuKho
    Private hasChecked As Boolean = False
    Private Sub NapLoaiDat1()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD1.DisplayMember = "kyhieu"
        CmbLD1.ValueMember = "kyhieu"
        CmbLD1.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiBienDong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from LOAIBD", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiBD.DisplayMember = "TenLoaiBD"
        CmbLoaiBD.ValueMember = "maBD"
        CmbLoaiBD.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbHuyen.DisplayMember = "T_huyen"
        CmbHuyen.ValueMember = "maH"
        CmbHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapPhong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMPhong", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        cmbPhong.DisplayMember = "Tenphong"
        cmbPhong.ValueMember = "maPhongID"
        cmbPhong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapNgan()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMNgan", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbNgan.DisplayMember = "TenNgan"
        CmbNgan.ValueMember = "maNganID"
        CmbNgan.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKe.DisplayMember = "TenKe"
        CmbKe.ValueMember = "maKeID"
        CmbKe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKho()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKho", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKho.DisplayMember = "TenKho"
        CmbKho.ValueMember = "maKhoID"
        CmbKho.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapTinhTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTinhTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbTinhTrang.DisplayMember = "TenTinhTrang"
        CmbTinhTrang.ValueMember = "maTinhTrangID"
        CmbTinhTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewDonViHanhChinh()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  maBDid,  bd.ngayBD, bd.ChuSD1,bd.cho_o1, bd.toBD, bd.sothua1,bd.dientich1, " &
                                " bd.loaidat1id, bd.NoiDungBD, " &
                                "lbd.tenloaibd , p.tenphong, ng.tenngan, k.tenke, kh.tenkho, tt.TenTinhTrang " &
                                " From DMTinhTrang tt, DMPhong p, DMNgan ng, DMKe k, DMKho kh,loaiBD lbd, BIENDONG bd " &
                                " WHERE bd.loaibdid = lbd.mabd and diachihuyenid= '" & CmbHuyen.SelectedValue.ToString & "' " &
                                " and diachixaid= '" & CmbXa.SelectedValue.ToString & "' " &
                                " and bd.maphongID = p.maphongid and bd.manganid = ng.manganid " &
                                " and bd.makeid = k.makeid and bd.makhoid = kh.makhoid and bd.tinhTrangid= tt.matinhtrangid and bd.maphongID = '0' ", ob_cnn) '
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewBD.DataSource = tbl
        '  Tieude()
        If tbl.Rows.Count = 0 Then
            MessageBox.Show("Không tìm thấy thông tin !")
            Return
        End If
        ob_cnn.Close()
    End Sub
    Private Sub CmbHuyen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbHuyen.SelectedIndexChanged
        ' load ten huyen
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & CmbHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbXa.DisplayMember = "TenXa"
        CmbXa.ValueMember = "maxa"
        CmbXa.DataSource = tbl
        ob_cnn.Close()

    End Sub
    Private Sub LoatLenTextBox()
        TextMaBDid.DataBindings.Clear()
        TextMaBDid.DataBindings.Add("Text", DataGridViewBD.DataSource, "maBDid")
        ChuSD1.DataBindings.Clear()
        ChuSD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "chusd1")
        DateNgayBD.DataBindings.Clear()
        DateNgayBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "Ngaybd")
        TextToBD1.DataBindings.Clear()
        TextToBD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "tobd")
        TextSoThua1.DataBindings.Clear()
        TextSoThua1.DataBindings.Add("Text", DataGridViewBD.DataSource, "sothua1")
        TextDienTich1.DataBindings.Clear()
        TextDienTich1.DataBindings.Add("Text", DataGridViewBD.DataSource, "dientich1")
        TextNoiDungBD.DataBindings.Clear()
        TextNoiDungBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "noidungbd")
        TextDiaChiTruoc.DataBindings.Clear()
        TextDiaChiTruoc.DataBindings.Add("Text", DataGridViewBD.DataSource, "cho_o1")
        CmbLD1.DataBindings.Clear()
        CmbLD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "loaidat1id")
        CmbLoaiBD.DataBindings.Clear()
        CmbLoaiBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenloaibd")
        cmbPhong.DataBindings.Clear()
        cmbPhong.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenphong")
        CmbNgan.DataBindings.Clear()
        CmbNgan.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenngan")
        CmbKe.DataBindings.Clear()
        CmbKe.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenke")
        CmbKho.DataBindings.Clear()
        CmbKho.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenkho")
        CmbTinhTrang.DataBindings.Clear()
        CmbTinhTrang.DataBindings.Add("Text", DataGridViewBD.DataSource, "tentinhtrang")
    End Sub

    Private Sub DataGridViewBD_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellClick
        LoatLenTextBox()
    End Sub

    Private Sub Update_Du_lieu()
        Try
            Dim Sql As String = ""
            Sql = " Update BIENDONG Set diachixaid = '" & CmbXa.SelectedValue.ToString & "', " &
                " diachiHuyenid = '" & CmbHuyen.SelectedValue.ToString & "'," &
                " loaidat1id ='" & CmbLD1.SelectedValue.ToString & "', " &
                " dientich1 = '" & TextDienTich1.Text & "'," &
                " sothua1 ='" & TextSoThua1.Text & "', " &
                " toBD ='" & TextToBD1.Text & "', " &
                " loaibdid = '" & CmbLoaiBD.SelectedValue.ToString & "'," &
                " cho_o1 = N'" & TextDiaChiTruoc.Text & "'," &
                " NoiDungBD =  N'" & TextNoiDungBD.Text & "'," &
                " ChuSD1 = N'" & ChuSD1.Text & "'," &
                " maPhongId = N'" & cmbPhong.SelectedValue.ToString & "' , " &
                " maNganId = N'" & CmbNgan.SelectedValue.ToString & "' , " &
                " maKeId = N'" & CmbKe.SelectedValue.ToString & "' , " &
                " maKhoId = N'" & CmbKho.SelectedValue.ToString & "' , " &
                " TinhTrangId = N'" & CmbTinhTrang.SelectedValue.ToString & "' , " &
                " ngayBD = " & Format(DateNgayBD.Value.ToOADate - 2) & " where " &
                " maBDid = '" & TextMaBDid.Text & "'"

            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()

            MsgBox("Nhập kho thành công!")

            NapDataGridViewDonViHanhChinh()

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub QLyCapNhat()
        Dim NguoiNhapKho As String
        NguoiNhapKho = ModuleTenDangNhap.m_tennhap
        Dim Sql As String = ""
        Sql = " insert into QL_NhapKho (maBDid, NgayNhapKho, NguoiNhapKho) values ( '" & TextMaBDid.Text & "' ," & Format(Now.ToOADate() - 2) & ", '" & NguoiNhapKho & "') "
        Dim cmd As New SqlCommand(Sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()
    End Sub

    Private Sub FrmCapNhatLuuKho_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NapDonViHuyen()
        NapLoaiDat1()
        NapLoaiBienDong()
        NapPhong()
        NapNgan()
        NapKe()
        NapKho()
        NapTinhTrang()
        DataGridViewBD.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBD.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Private Sub Buttonlamtuoi_Click(sender As Object, e As EventArgs) Handles Buttonlamtuoi.Click
        Try
            NapDataGridViewDonViHanhChinh()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub ButtonLuu_Click(sender As Object, e As EventArgs) Handles ButtonLuu.Click
        If ChuSD1.Text = "" Then
            MessageBox.Show("Bạn Chưa chọn chủ sử dụng để lưu trữ!")
            Return
        End If
        If cmbPhong.SelectedValue.ToString = "0" Then
            MessageBox.Show("Chưa chọn phong để lưu trữ !")
            Return
        End If
        If CmbNgan.SelectedValue.ToString = "0" Then
            MessageBox.Show("Chưa chọn Ngăn để lưu trữ !")
            Return
        End If
        If CmbKe.SelectedValue.ToString = "0" Then
            MessageBox.Show("Chưa chọn Kệ để lưu trữ !")
            Return
        End If
        If CmbKho.SelectedValue.ToString = "0" Then
            MessageBox.Show("Chưa chọn Kho để lưu trữ !")
            Return
        Else
            QLyCapNhat()
            Update_Du_lieu()
            LoatLenTextBox()
            NapPhong()
            NapNgan()
            NapKe()
            NapKho()
        End If

    End Sub

    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub

    Private Sub Label17_Click(sender As Object, e As EventArgs) Handles Label17.Click

    End Sub

    Private Sub DataGridViewBD_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridViewBD.CellFormatting
        '   DataGridViewBD.Rows(e.RowIndex).HeaderCell.Value = CStr(e.RowIndex + 1)
    End Sub

    Private Sub DataGridViewBD_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellValueChanged
        ' Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        If e.RowIndex > -1 And e.ColumnIndex = 0 Then
            hasChecked = Convert.ToBoolean(DataGridViewBD(e.ColumnIndex, e.RowIndex).Value)
            If hasChecked Then
                For i As Integer = 0 To DataGridViewBD.RowCount - 1
                    'If e.RowIndex <> i And Convert.ToBoolean(DataGridViewBD(e.ColumnIndex, i).Value) Then
                    '   DataGridViewBD(e.ColumnIndex, i).Value = False
                    '   End If
                Next
            End If
        End If
    End Sub

    Private Sub DataGridViewBD_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellContentClick
        DataGridViewBD.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End Sub


    Private Sub DataGridViewBD_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBD.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBD.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBD.RowHeadersWidth < Size.Width + 20 Then DataGridViewBD.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))

    End Sub

    Private Sub CmbXa_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbXa.SelectedIndexChanged

    End Sub

    Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click

    End Sub

    Private Sub DateNgayBD_ValueChanged(sender As Object, e As EventArgs) Handles DateNgayBD.ValueChanged

    End Sub
End Class